package de.fme.tracking.twitter;

import java.io.Serializable;
import java.util.List;

import org.alfresco.repo.publishing.PublishingModel;
import org.alfresco.repo.publishing.twitter.TwitterChannelType;
import org.alfresco.service.cmr.publishing.PublishingEvent;
import org.alfresco.service.cmr.publishing.channels.Channel;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.social.connect.Connection;
import org.springframework.social.twitter.api.Tweet;
import org.springframework.social.twitter.api.Twitter;

import de.fme.tracking.FeedbackChannelType;

/**
 * 
 * @author PloetzJ
 * @since  4.0b
 * 
 * getting the Tweet ID
 */
public class TwitterFeedbackChannelType extends TwitterChannelType implements FeedbackChannelType
{
    private final static Log log = LogFactory.getLog(TwitterFeedbackChannelType.class);
    private NodeService nodeService;
    
    public void setNodeService(NodeService nodeService){
    	this.nodeService = nodeService;
    }
    
    /**
     * {@inheritDoc}
     */
    public String getLastStatusUpdateId(Channel channel)
    {
        Connection<Twitter> connection = getConnectionForChannel(channel.getNodeRef());
        Twitter twitterApi = connection.getApi();
        try {
            long numMillisecondsToSleep = 1000; // 1 seconds
            Thread.sleep(numMillisecondsToSleep);
        } catch (InterruptedException e) {
        }
        Long id = twitterApi.timelineOperations().getUserTimeline().iterator().next().getId();
        String statusId = id.toString();
		if (log.isInfoEnabled())
        {
            log.info("Last TweetId for statusUpdate of channel " + channel.getName() + ": " + statusId);
        }
		return statusId;
    }
    
    /**
     * Testing feedback collection
     * @param channel
     * @param id
     * @return
     */
    {
        
    }

	@Override
	public int collectFeedback() {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * 
	 */
	public int collectFeedbackForPubEvent(String pubEventId) {
		Connection<Twitter> connection = createConnectionForPubEvent(pubEventId);
        Twitter twitterApi = connection.getApi();
        Long tweetId = Long.parseLong(pubEventId);
        List<Tweet> retweets = twitterApi.timelineOperations().getRetweets(tweetId);
        int numberOfRetweets = 0;
        if(!retweets.isEmpty()){
        	numberOfRetweets = retweets.size();
        }
		if (log.isInfoEnabled())
        {
            log.info("Collecting feedback for tweetId " + tweetId);
        }
		String feedback = "Tweet with Id " + tweetId + " was retweeted " + numberOfRetweets + "times.";
		return 0;
	}

	private Connection<Twitter> createConnectionForPubEvent(String pubEventId) {
		// create NodeRef from Uuid and get channelProperty from the PubEvent
		NodeRef pubEventNode = nodeService.getNodeRef(Long.parseLong(pubEventId));
		nodeService.getProperty(pubEventNode, PublishingModel.PROP_PUBLISHING_EVENT_CHANNEL);
		// TODO get channel and create connection
		return null;
	}

	@Override
	public int collectFeedbackForChannel(String channelId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int collectComments() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int collectCommentsForPubEvent(String pubEventId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int collectCommentsForChannel(String channelId) {
		// TODO Auto-generated method stub
		return 0;
	}
   
    
}
